package com.fastloan.mgt.expert.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.expert.dao.ExpertProductMapper;
import com.fastloan.mgt.expert.model.Expert;
import com.fastloan.mgt.expert.model.vo.ExpertVO;
import com.fastloan.mgt.expert.model.vo.UserExpertExtVO;
import com.fastloan.mgt.expert.service.ExpertProductService;
import com.fastloan.mgt.expert.model.ExpertProduct;
import com.fastloan.mgt.expert.service.ExpertService;
import com.fastloan.mgt.product.basics.model.Product;
import com.fastloan.mgt.product.basics.service.ProductService;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.fastloan.utils.DozerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

/**
 * @author admins createTime 2017/11/10
 */
@Service
public class ExpertProductServiceImpl extends BasicServiceImpl<ExpertProduct> implements ExpertProductService {

    @Autowired
    private ExpertProductMapper expertProductMapper;

    @Autowired
    private ExpertService expertService;

    @Autowired
    private CmuUserService cmuUserService;

    @Autowired
    private ProductService productService;

    @Override
    public Integer insertList(List<ExpertProduct> list) {
        return expertProductMapper.insertList(list);
    }

    @Override
    @Transactional
    public Integer updateList(List<ExpertProduct> list) {
        String productId = list.get(0).getProductId();
        ExpertProduct expertProduct = new ExpertProduct();
        expertProduct.setProductId(productId);
        expertProductMapper.delete(expertProduct);
        return insertList(list);
    }

    @Override
    public List<ExpertVO> queryExpertByProductId(String productId) {
        ExpertProduct expertProduct = new ExpertProduct();
        expertProduct.setProductId(productId);
        List<ExpertVO> expertVOList = new ArrayList<>();
        List<List<ExpertVO>> listList = select(expertProduct)
                .stream()
                .map(product -> {
                    String productName = productService.selectByPrimaryKey(product.getProductId()).getProductName();
                    return getExpert(product, productName);
                }).collect(toList());
        listList.forEach(expertVOList::addAll);
        return expertVOList;
    }

    @Override
    public List<ExpertProduct> queryExpert(String productId) {
        ExpertProduct expertProduct = new ExpertProduct();
        expertProduct.setProductId(productId);
        return select(expertProduct);
    }


    private List<ExpertVO> getExpert(ExpertProduct function, String productName) {
        return Arrays.stream(function.getExpertId().split(","))
                .map(expertService::selectByPrimaryKey)
                .map(Expert::getUserId)
                .map(cmuUserService::selectByPrimaryKey)
                .map(cmuUser -> {
                    ExpertVO expertVO = DozerUtil.map(function, ExpertVO.class);
                    expertVO.setUserName(cmuUser.getCname());
                    expertVO.setUserId(cmuUser.getId());
                    expertVO.setProductName(productName);
                    return expertVO;
                }).collect(toList());
    }


}
